package com.syni.mdd.yhd.common.all.listen.groupBuy;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsUserOperationLog;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.service.BmsUserOperationLogService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
@Log4j2
public class GroupBuyDetailListener implements ApplicationListener<GetGroupBuyDetailEvent> {

    @Autowired
    BmsUserOperationLogService bmsUserOperationLogService;

    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;

    @Override
    public void onApplicationEvent(GetGroupBuyDetailEvent getGroupBuyDetailEvent) {
        BmsDxGroupBuy bmsDxGroupBuy = getGroupBuyDetailEvent.getBmsDxGroupBuy();
        bmsDxGroupBuy.setViewsNum(bmsDxGroupBuy.getViewsNum() == null ? 1 : (bmsDxGroupBuy.getViewsNum() + 1));
        bmsDxGroupBuyService.getBaseDao().save(bmsDxGroupBuy);
        Integer userId = getGroupBuyDetailEvent.getUserId();
        BmsUserOperationLog bmsUserOperationLog = new BmsUserOperationLog();
        bmsUserOperationLog.setUserId(userId);
        bmsUserOperationLog.setOperationTime(new Date());
        bmsUserOperationLog.setOperationType(Constants.GROUP_BUY_DETAIL);
        bmsUserOperationLog.setOperationName("用户点击团购详情");
        bmsUserOperationLog.setFromOrigin(Constants.USER_CLIENT);
        Map<String,Object> content = new HashMap<>();
        content.put("groupBuyId",bmsDxGroupBuy.getId());
        content.put("businessId",bmsDxGroupBuy.getBusinessId());
        bmsUserOperationLog.setOperationContent(JSON.toJSONString(content));
        bmsUserOperationLogService.getBaseDao().save(bmsUserOperationLog);

        //
    }
}
